<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png"/>
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container :style="{height: containerheight}">
      <!-- 侧边栏 -->
      <el-aside :width="flag ? '64px' : '200px' ">
        <div class="toggle-flag" @click="toggleFlag">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu background-color="#333744" 
        text-color="#fff" active-text-color="#409EFF" 
        unique-opened :collapse="flag" :collapse-transition="false"
        router :default-active="activePath">
          <el-submenu :index="item.id +'' " v-for="item in menuList" :key="item.id">
            <!-- 一级菜单 -->
            <template slot="title">
              <i :class="iconobj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subitem.path" :key="subitem.id" v-for="subitem in item.children" 
            @click="saveNavState('/'+subitem.path)">
              <!-- 图标 -->
              <i class="iconfont icon-tijikongjian"></i>
              <!-- 文本 -->
              <span>{{subitem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边主体部分 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconobj: {
        "125": "iconfont icon-users",
        "103": "iconfont icon-lock_fill",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      // 是否折叠
      flag: false,
      // 被激活的链接地址
      activePath: '',
      // 给页面头部下面定一个默认高度
      containerheight: '565px'
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  mounted() {
    this.containerheight = (window.innerHeight-60)+'px'
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
      this.$message.success("退出成功");
    },
    // 获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
        return false;
      }
      this.menuList = res.data;
      // console.log(res);
    },
    // 点击按钮,切换菜单的折叠与展开
    toggleFlag(){
      this.flag = !this.flag
    },
    //保存链接的激活状态
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath);
      this.activePath = activePath;
    }
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-container {
    height: 100%;
  }
  .el-header {
    background: #373d41;
    padding-left: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    div {
      display: flex;
      align-items: center;
      img {
        margin-right: 20px;
      }
      span {
        font-size: 20px;
        font-weight: 600;
      }
    }
  }
  .el-aside {
    background: rgb(51, 55, 68);
    .el-menu {
      border: 0;
    };
    .toggle-flag {
      color: #fff;
      font-size: 12px;
      text-align: center;
      line-height: 25px;
      background-color: #4a5064;
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
      cursor: pointer;
      letter-spacing: .1em;
    };
    .iconfont {
        margin-right: 10px;
    }
  }
}
</style>